package com.litidea.actions;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.opensymphony.xwork2.Action;
import com.litidea.commons.utils.CollectionUtils;
import com.litidea.commons.utils.MenuUtils;
import com.litidea.dao.SystemDAO;
import com.litidea.dataobjects.MenuNodeDO;
import com.litidea.dataobjects.RoleDO;
import com.litidea.dataobjects.UserLoginDO;

public class LoginAction extends BaseAction implements Action {
	
	private SystemDAO systemDAO;
	

	/**
	 * 序列码
	 */
	private static final long serialVersionUID = -6406168181463247710L;
	
	public void setSystemDAO(SystemDAO systemDAO) {
		this.systemDAO = systemDAO;
	}
	
	public String adminToLogin() {
		return SUCCESS;
	}

	/**
	 * 一级菜单
	 * @return
	 */
	public String firstMenus() {
		UserLoginDO userLoginDO = this.findUserInfo();
		List<MenuNodeDO> list = systemDAO.getFirstPrivMenus(userLoginDO.getId());
		return responseAjax(list);
	}
	
	/**
     * 获取菜单列表
     * @return
     */
    public String getFavorite() {
    	UserLoginDO userLoginDO = this.findUserInfo();
    	//String wfKey = getString("allParentId");
//    	HttpServletRequestExtend request = new HttpServletRequestExtend();
//    	request.addParameter("allParentId", "{1}");
//    	ServletActionContext.setRequest(request);
        List<MenuNodeDO> menus = systemDAO.getMenuChilds(getString("allParentId") , userLoginDO.getId());
        return responseAjax(MenuUtils.convertMenus(menus, 2));
    }
    
    /**
	 * 用户的roleKey
	 * @return
	 */
	public String getUserRoleKeys() {
		UserLoginDO userLoginDO = this.findUserInfo();
		List<RoleDO>roles = systemDAO.getUserRoles(String.valueOf(userLoginDO.getId()));
		if(CollectionUtils.isEmpty(roles)) return "";
		StringBuilder buf = new StringBuilder(roles.size() * 6);
		for(RoleDO role : roles) {
			if(StringUtils.isNotEmpty(role.getRoleKey())) {
				buf.append(role.getRoleKey()).append(",");
			}
		}
		if(buf.length() > 0) buf.deleteCharAt(buf.length() - 1);
		return buf.toString();
	}

}
